
package people

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/people"
    peopleReq "github.com/flipped-aurora/gin-vue-admin/server/model/people/request"
)

type GoodsService struct {}
// CreateGoods 创建goods表记录
// Author [yourname](https://github.com/yourname)
func (goodsService *GoodsService) CreateGoods(ctx context.Context, goods *people.Goods) (err error) {
	err = global.GVA_DB.Create(goods).Error
	return err
}

// DeleteGoods 删除goods表记录
// Author [yourname](https://github.com/yourname)
func (goodsService *GoodsService)DeleteGoods(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&people.Goods{},"id = ?",ID).Error
	return err
}

// DeleteGoodsByIds 批量删除goods表记录
// Author [yourname](https://github.com/yourname)
func (goodsService *GoodsService)DeleteGoodsByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]people.Goods{},"id in ?",IDs).Error
	return err
}

// UpdateGoods 更新goods表记录
// Author [yourname](https://github.com/yourname)
func (goodsService *GoodsService)UpdateGoods(ctx context.Context, goods people.Goods) (err error) {
	err = global.GVA_DB.Model(&people.Goods{}).Where("id = ?",goods.ID).Updates(&goods).Error
	return err
}

// GetGoods 根据ID获取goods表记录
// Author [yourname](https://github.com/yourname)
func (goodsService *GoodsService)GetGoods(ctx context.Context, ID string) (goods people.Goods, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&goods).Error
	return
}
// GetGoodsInfoList 分页获取goods表记录
// Author [yourname](https://github.com/yourname)
func (goodsService *GoodsService)GetGoodsInfoList(ctx context.Context, info peopleReq.GoodsSearch) (list []people.Goods, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&people.Goods{})
    var goodss []people.Goods
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&goodss).Error
	return  goodss, total, err
}
func (goodsService *GoodsService)GetGoodsPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
